﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Furion.LinqBuilder;
using JQData.Core.TushareSDK.Models;

namespace JQData.Core.TushareSDK
{
    public class OtherApi
    {
        private readonly TushareCore _core;

        public OtherApi(TushareCore core)
        {
            _core = core;
        }

        /// <summary>
        /// 获取游资分类名录信息
        /// </summary>
        /// <param name="name">游资名称</param>
        /// <returns>游资名录列表</returns>
        public async Task<List<HmListItem>> GetHmListAsync(string name = null)
        {
            var parameters = new Dictionary<string, object>();
            if (!string.IsNullOrEmpty(name))
            {
                parameters.Add("name", name);
            }

            // Assuming TushareCore has a method CallApiAsync that can handle a dictionary of parameters
            // and the API returns a structure that can be deserialized into HmListResult.
            // The actual call might look different based on TushareCore's implementation.
            // For now, we'll assume it can take the API name and parameters, and deserialize the items directly.
            var items = await _core.CallApiAsync<List<string>>("hm_list", parameters);

            var list = new List<HmListItem>();

            foreach (var item in items.Data.Items)
            {
                var 营业部 = JsonSerializer.Deserialize<List<string>>(item[2]);
                var hmItem = new HmListItem()
                {
                    Name = item[0],
                    Desc = item[1],
                    Orgs = "|" + string.Join("|", 营业部) + "|"
                };
                list.Add(hmItem);
            }


            return list;
        }


        /// <summary>
        /// 获取游资分类名录信息
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>游资名录列表</returns>
        public async Task<List<HmDetailResult>> GetHmDetailAsync(DateOnly? date = null)
        {
            var parameters = new Dictionary<string, object>();
            if (date != null)
            {
                parameters.Add("trade_date", date.Value.ToString("yyyyMMdd"));
            }

            // Assuming TushareCore has a method CallApiAsync that can handle a dictionary of parameters
            // and the API returns a structure that can be deserialized into HmListResult.
            // The actual call might look different based on TushareCore's implementation.
            // For now, we'll assume it can take the API name and parameters, and deserialize the items directly.
            var items = await _core.CallApiAsync<List<object>>("hm_detail", parameters, "trade_date,ts_code,ts_name,buy_amount,sell_amount,net_amount,hm_name,hm_orgs,tag");

            var list = new List<HmDetailResult>();


            foreach (var item in items.Data.Items)
            {
                //var 营业部 = JsonSerializer.Deserialize<List<string>>(item[2]);
                var hmItem = new HmDetailResult()
                {
                    Date = DateOnly.ParseExact(item[0].ToString(), "yyyyMMdd"),
                    StockCode = item[1].ToString(),
                    StockName = item[2].ToString(),
                    BuyAmount = decimal.Parse(item[3].ToString()),
                    SellAmount = decimal.Parse(item[4].ToString()),
                    NetAmount = decimal.Parse(item[5].ToString()),
                    HmName = item[6].ToString(),
                    Tag = item[7].ToString()
                };
                list.Add(hmItem);
            }


            return list;
        }



    }
}
